package com.talent.core.protocol.mqtt.codec.decoder;

import com.talent.core.protocol.mqtt.enums.TlMqttQos;
import com.talent.core.protocol.mqtt.model.fix.TlMqttFixedHead;
import com.talent.core.protocol.mqtt.model.request.TlMqttPubRecReq;
import com.talent.core.protocol.mqtt.model.variable.TlMqttPubRecVariableHead;
import io.netty.buffer.ByteBuf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName：TlConnectDecoder
 * @Author: hszhou
 * @Date: 2024/11/30 15:12
 * @Description: Qos2的消息的消息回复
 */
public class TlMqttPubRecDecoder  {

    private static final  Logger logger = LoggerFactory.getLogger(TlMqttPubRecDecoder.class);

    public TlMqttPubRecReq build(ByteBuf buf, int type, int remainingLength){
        TlMqttFixedHead fixedHead = decodeFixedHeader( type, remainingLength);
        int qos = fixedHead.getQos();
        TlMqttQos tlMqttQos = TlMqttQos.valueOf(qos);
        TlMqttPubRecVariableHead variableHead = decodeVariableHeader(buf,tlMqttQos);
        return new TlMqttPubRecReq(fixedHead,variableHead);

    }

    TlMqttFixedHead decodeFixedHeader(int type,int remainingLength) {
        TlMqttFixedHead fixedHead =new TlMqttFixedHead();
        int messageType = type >> 4;
        fixedHead.setMessageType(messageType);
        fixedHead.setType(type);
        fixedHead.setLength(remainingLength);
        return fixedHead;
    }


    TlMqttPubRecVariableHead decodeVariableHeader(ByteBuf buf, TlMqttQos qos) {
        TlMqttPubRecVariableHead variableHead=new TlMqttPubRecVariableHead();
        int messageId = buf.readUnsignedShort();
        variableHead.setMessageId(messageId);
        return variableHead;
    }

}
